package top.aylan.org.ctrl.admin;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import top.aylan.org.entity.User;
import top.aylan.org.service.UserService;
import top.aylan.org.tools.Pager;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2018/12/20.
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminMemberController {

    @Resource
    UserService userService;

    @RequestMapping(value = "/user/index")
    public String index(ModelMap modelMap, @RequestParam(value = "page",defaultValue = "1") int page){

        Pager<User> pager = new Pager<User>();
        pager.setCurrentPage(page);
        try {
            List<User> list =  userService.selectUsersByPage(pager);
            pager.setList(list);
            modelMap.addAttribute("pager",pager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin/users";
    }


    @RequestMapping(value = "/user/delete")
    public String delete(@RequestParam(value = "id",defaultValue = "0") Integer id){

        try {
            userService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/user/index";
    }

}
